#include "../include/FileDataSource.h"
#include <fstream>

using namespace std;

FileDataSource::FileDataSource(std::string rootPath)
{
	m_strRootPath=rootPath;
}

FileDataSource::~FileDataSource(void)
{

}

int FileDataSource::GetStatusJson(char byteBuffe[],int& bufferSize) 
{
	memset(byteBuffe,0,bufferSize);
	string strStatusPath=m_strRootPath+"/status.json";
	std::ifstream ifs(strStatusPath.c_str(),std::ios_base::in | std::ios_base::binary);
	// get length of file:
	if (!ifs.good())
	{
		return -1;
	}

	ifs.seekg (0, ios::end);
	int length = ifs.tellg();
	ifs.seekg (0, ios::beg);

	ifs.read (byteBuffe,length);
	bufferSize=length;
	ifs.close();

	return 0;
	
}
int FileDataSource::GetSlotsJson(char byteBuffer[],int& bufferSize) 
{
	memset(byteBuffer,0,bufferSize);
	string strSlotsPath = m_strRootPath+"/slots.json";
	std::ifstream ifs(strSlotsPath.c_str(),std::ios_base::in | std::ios_base::binary);
	// get length of file:
	if (!ifs.good())
	{
		return -1;
	}

	ifs.seekg (0, ios::end);
	int length = ifs.tellg();
	ifs.seekg (0, ios::beg);

	ifs.read (byteBuffer,length);
	bufferSize=length;
	ifs.close();

	return 0;
}
int FileDataSource::GetAdListJson(int soltId,int maxResultCount,char byteBuffer[],int& bufferSize)
{
	memset(byteBuffer,0,bufferSize);
	char strSlotId[100];
	memset(strSlotId,0,100);
	_itoa_s(soltId,strSlotId,10);
	string strAdListPath = m_strRootPath+"/"+strSlotId+".json";
	std::ifstream ifs(strAdListPath.c_str(),std::ios_base::in | std::ios_base::binary);
	// get length of file:
	if (!ifs.good())
	{
		return -1;
	}

	ifs.seekg (0, ios::end);
	int length = ifs.tellg();
	ifs.seekg (0, ios::beg);

	ifs.read (byteBuffer,length);
	bufferSize=length;
	ifs.close();

	return 0;	
}
